package com.t9101DR.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.coyote.http2.HPackHuffman;

import com.t9101DR.bean.DrUser;
import com.t9101DR.bean.RoomAddress;
import com.t9101DR.dao.BaseDao;
import com.t9101DR.dao.DrUserDao;

public class DrUserDaoImpl extends BaseDao implements DrUserDao {

	@Override
	public List<DrUser> list(DrUser du) {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List<DrUser> list=new ArrayList<DrUser>();
		try {
			conn=getConnection();
			String sql="SELECT DrUserId,userid,addArea,DrName,userName,telephone,sex,loving,loveText,birthday,constellation,blood,profession,address,hobby,password,IDcard FROM `DrUser` WHERE 1=1";
			if(du!=null&&du.getDrUserId()!=0) {
				sql=sql+" and DrUserId= "+du.getDrUserId()+" ";
			}
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()) {
				DrUser bren=new DrUser();
				bren.setDrUserId(rs.getInt(1));
				bren.setUserid(rs.getString(2));
				bren.setDrName(rs.getString(3));
				bren.setTelephone(rs.getString(4));
				bren.setLoving(rs.getString(5));
				bren.setLoveText(rs.getString(6));
				bren.setBirthday(rs.getString(7));
				bren.setConstellation(rs.getString(8));
				bren.setBlood(rs.getString(9));
				bren.setProfession(rs.getString(10));
				bren.setAddress(rs.getString(11));
				bren.setHobby(rs.getString(12));
				bren.setPassword(rs.getString(13));
				bren.setIdCard(rs.getString(14));
				list.add(bren);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeAll(conn, pstmt, rs);
		}
		return list;
	}

	@Override
	public int add(DrUser du) {
		int count =0;
		String sql="INSERT INTO DrUser(userid,DrName,userName,telephone,sex,loving,loveText,birthday,constellation,blood,profession,address,hobby,password,IDcard) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ";
		Object [] pare= {du.getUserid(),du.getDrName(),du.getDrUserId(),du.getUserName(),du.getTelephone(),du.getSex(),du.getLoving(),du.getLoveText(),du.getBirthday(),du.getConstellation(),du.getBlood(),du.getProfession(),du.getAddress(),du.getHobby(),du.getPassword(),du.getIdCard()};
		count=executeUpdate(sql, pare);
		return count;
	}

	@Override
	public int update(DrUser du) {
		int count =0;
		String sql=" UPDATE DrUser set userid=?,DrName=?,userName=?,telephone=?,sex=?,loving=?,loveText=?,birthday=?,constellation=?,blood=?,profession=?,address=?,hobby=?,password=?,IDcard=? WHERE DrUserId=?  ";
		Object [] params = {du.getUserid(),du.getDrName(),du.getDrUserId(),du.getUserName(),du.getTelephone(),du.getSex(),du.getLoving(),du.getLoveText(),du.getBirthday(),du.getConstellation(),du.getBlood(),du.getProfession(),du.getAddress(),du.getHobby(),du.getPassword(),du.getIdCard()};
		 count =this.executeUpdate(sql, params);
			return count;
	}

	@Override
	public int deleteById(int id) {
		// TODO Auto-generated method stub
		return 0;
	}

}
